package com.mcflies.ui.menu;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.mcflies.commons.ImitateUCMenuGridViewAdapter;
import com.mcflies.commons.ImitateUCMenuItemData;
import com.mcflies.commons.ImitateUCMenuMainLVAdapter;
import com.mcflies.ui.R;

/**
 * @author:mcfly_jun
 * @create_date:2012-8-9
 * @comment:模仿UC浏览器的Menu
 */
public class ImitateUCMenuActivity extends Activity {
	private ListView imitateUCMenuMain_LV;
	private String[] imitateUCMenuMainLVArray;// ListView数据源

	private GridView imitateUCMenuMain_GV;// 底部菜单
	private ImitateUCMenuGridViewAdapter imitateUCMenuMainBottomAdapter;// 底部菜单对应适配器
	private ImitateUCMenuItemData imitateUCMenuItemData;// 底部菜单对应菜单数据
	private String[] imitateUCMenuMainToolBarArray;// 底部菜单对应菜单标题数据

	private String[] imitateUCMenuMainToolBarArray1;
	private ImitateUCMenuItemData imitateUCMenuItemData1;

	private String[] imitateUCMenuMainToolBarArray2;
	private ImitateUCMenuItemData imitateUCMenuItemData2;
	private View popupMenuView; // 弹出菜单视图
	private GridView popupMenuGridView;// 弹出菜单视图中的GridView
	private ImitateUCMenuGridViewAdapter popupMenuGridViewAdapter;// 弹出菜单中的GridView适配器
	private PopupWindow popupWindow;

	private final int SWITCH_MENU = 4;
	private final int POP_MENU_ITEM_MORE = 11;
	private boolean isShowMore = true;// 弹出菜单更多菜单控制

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imitate_uc_menu_main);
		initWidget();
		initData();
		// 初始化底部菜单
		initBottomToolBar();
		// 初始化弹出框
		initPopupMenu();
		// 对ListView进行初始化
		initImitateUCMenuMainLV();
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {// 截获菜单事件
		showOrHideMenuWindow();
		return false;// 返回为true 则显示系统menu
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("menu");// 必须创建一项
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * @commont:初始化控件
	 */
	private void initWidget() {
		imitateUCMenuMain_LV = (ListView) findViewById(R.id.imitateUCMenuMain_LV);
		imitateUCMenuMain_GV = (GridView) findViewById(R.id.imitateUCMenuMain_GV);
	}

	/**
	 * @commont:初始化数据
	 */
	private void initData() {
		imitateUCMenuMainLVArray = getResources().getStringArray(
				R.array.imitateUCMenuMainLVArray);
		imitateUCMenuMainToolBarArray = getResources().getStringArray(
				R.array.imitateUCMenuMainToolBarArray);
		imitateUCMenuMainToolBarArray1 = getResources().getStringArray(
				R.array.imitateUCMenuMainToolBarArray1);
		imitateUCMenuMainToolBarArray2 = getResources().getStringArray(
				R.array.imitateUCMenuMainToolBarArray2);
	}

	/**
	 * @commont:初始化ListView相关信息
	 */
	private void initImitateUCMenuMainLV() {
		imitateUCMenuMain_LV.setAdapter(new ImitateUCMenuMainLVAdapter(this,
				imitateUCMenuMainLVArray));
		imitateUCMenuMain_LV.setCacheColorHint(0x00000000); // 设置拖动时保存背景色
	}

	/**
	 * @commont:初始化底部菜单
	 */
	private void initBottomToolBar() {
		LevelListDrawable levelListDrawable = (LevelListDrawable) getResources()
				.getDrawable(R.drawable.imitate_uc_menu_main_toolbar_image_list);

		imitateUCMenuItemData = new ImitateUCMenuItemData(levelListDrawable,
				imitateUCMenuMainToolBarArray,
				imitateUCMenuMainToolBarArray.length);
		imitateUCMenuMainBottomAdapter = new ImitateUCMenuGridViewAdapter(this,
				imitateUCMenuItemData);
		imitateUCMenuMain_GV.setAdapter(imitateUCMenuMainBottomAdapter);

		imitateUCMenuMain_GV.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long id) {
				switch (position) {
				case SWITCH_MENU:
					showOrHideMenuWindow();
					break;
				}
			}
		});
		imitateUCMenuMain_GV.setSelection(SWITCH_MENU);
	}

	/**
	 * @commont:初始化弹出框菜单
	 */
	private void initPopupMenu() {
		LevelListDrawable levelListDrawable1 = (LevelListDrawable) getResources()
				.getDrawable(
						R.drawable.imitate_uc_menu_main_toolbar_image_list1);
		imitateUCMenuItemData1 = new ImitateUCMenuItemData(levelListDrawable1,
				imitateUCMenuMainToolBarArray1,
				imitateUCMenuMainToolBarArray1.length);

		LevelListDrawable levelListDrawable2 = (LevelListDrawable) getResources()
				.getDrawable(
						R.drawable.imitate_uc_menu_main_toolbar_image_list2);
		imitateUCMenuItemData2 = new ImitateUCMenuItemData(levelListDrawable2,
				imitateUCMenuMainToolBarArray2,
				imitateUCMenuMainToolBarArray2.length);

		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		popupMenuView = inflater.inflate(
				R.layout.imitate_uc_menu_main_popup_menu, null);

		popupMenuGridView = (GridView) popupMenuView
				.findViewById(R.id.imitateUCMenuMainPopupMenu_GV);
		popupMenuGridViewAdapter = new ImitateUCMenuGridViewAdapter(this,
				imitateUCMenuItemData1);
		popupMenuGridView.setAdapter(popupMenuGridViewAdapter);
		// 添加点击事件监听器
		popupMenuGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long id) {
				switch (position) {
				case POP_MENU_ITEM_MORE:
					if (isShowMore) {
						popupMenuGridViewAdapter
								.refreshMenuItemData(imitateUCMenuItemData2);
						isShowMore = false;
					} else {
						popupMenuGridViewAdapter
								.refreshMenuItemData(imitateUCMenuItemData1);
						isShowMore = true;
					}
					popupMenuGridView.invalidate();
					break;
				}
			}
		});

		popupMenuGridView.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_MENU:
					showOrHideMenuWindow();
					break;
				}
				return false;
			}
		});

		// 弹出框
		popupWindow = new PopupWindow(popupMenuView, LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		popupWindow.setFocusable(true);// 如果没有获得焦点menu菜单中的控件事件无法响应

		ColorDrawable cd = new ColorDrawable(0xb0000000);
		popupWindow.setBackgroundDrawable(cd);
		popupWindow.setAnimationStyle(android.R.style.Animation_Toast);
	}

	// 弹出菜单窗口
	private void showOrHideMenuWindow() {
		if (popupWindow.isShowing()) {
			popupWindow.dismiss();
			isShowMore = true;
		} else {
			popupMenuGridViewAdapter
					.refreshMenuItemData(imitateUCMenuItemData1);
			popupWindow.showAtLocation(findViewById(R.id.imitateUCMenuMain_RL),
					Gravity.TOP, 0, 0);
		}
	}
}
